package com.example.test.model.social_api;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test.activity.FriLineActivity;
import com.example.test.model.Constants;
import com.example.test.model.json_analyzer.QQJsonAnalyzer;
import com.tencent.weibo.sdk.android.api.FriendAPI;
import com.tencent.weibo.sdk.android.api.TimeLineAPI;
import com.tencent.weibo.sdk.android.api.UserAPI;
import com.tencent.weibo.sdk.android.api.WeiboAPI;
import com.tencent.weibo.sdk.android.api.util.Util;
import com.tencent.weibo.sdk.android.model.AccountModel;
import com.tencent.weibo.sdk.android.model.GlobalVar;
import com.tencent.weibo.sdk.android.model.ModelResult;
import com.tencent.weibo.sdk.android.network.HttpCallback;

public class QQAPI {
	
	 //获取单条微博资料
	public static void  getOneWeibo(final Context context,String weiboId,final Handler handler)
	{
		if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
			return;
		WeiboAPI api =new WeiboAPI(new AccountModel(GlobalVar.QQToken));
		api.show(context, weiboId, "json", new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				List<HashMap<String, String>> listArrays =	QQJsonAnalyzer.OneWeiboJsonToHasArr((ModelResult)object);
				if (listArrays==null) {
					Toast.makeText(context, "获取微博失败", 1000).show();
				}
				else
				{
					sendMsg(handler, Constants.MSG_GETONEWEIBO_SUCCESS, listArrays);
				}
			}
		}, null, 4);
		api.reList(context, "json", 0, weiboId, 0, "0", 50, "0", new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				List<HashMap<String, String>> listArrays =	QQJsonAnalyzer.timeLineJsonToHasArr((ModelResult)object);
				if (listArrays==null) {
					
				}
				else
				{
					sendMsg(handler, Constants.MSG_GETREPOSTLIST_SUCCESS, listArrays);
				}
			}
		}, null, 4);
        api.reList(context, "json", 1, weiboId, 0, "0", 50, "0", new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				List<HashMap<String, String>> listArrays =	QQJsonAnalyzer.timeLineJsonToHasArr((ModelResult)object);
				if (listArrays==null) {
				}
				else
				{
					sendMsg(handler, Constants.MSG_GETCOMMENTLIST_SUCCESS, listArrays);
				}
			}
		}, null, 4);
		
	}
	//关注用户
			public static void follow(final Context context,final String fopenid,final Handler handler)
			{
				FriendAPI api=new FriendAPI(new AccountModel(GlobalVar.QQToken));
				api.addFriend(context, "json", null, fopenid, new HttpCallback() {
					
					@Override
					public void onResult(Object object) {
						ModelResult result=(ModelResult)object;
						if(result.isSuccess())
						{
							Message msg=new Message();
							msg.what=Constants.MSG_FOLLOW_SUCCESS;
							msg.obj=fopenid;
							handler.sendMessage(msg);
						}
						
					}
				}, null, 4);
			}
			//取消关注
			public static void cancelFollow(final Context context,final String openid,final Handler handler)
			{
				FriendAPI api=new FriendAPI(new AccountModel(GlobalVar.QQToken));
				api.delFriend(context, "json", null, openid, new HttpCallback() {
					
					@Override
					public void onResult(Object object) {
						Message msg=new Message();
						msg.what=Constants.MSG_CANCELFOLLOW_SUCCESS;
						msg.obj=openid;
						handler.sendMessage(msg);
					}
				}, null, 4);
			}
	//获取用户关注列表
		public static void getUserFollowList(final Context context,String openid,int reqnum,int sIndex,final Handler handler)
		{
			FriendAPI api=new FriendAPI(new AccountModel(GlobalVar.QQToken));
			api.userFriendIDolList(context, openid, "json", reqnum, sIndex, 1, 0, new HttpCallback() {
				
				@Override
				public void onResult(Object object) {
					List<HashMap<String, String>> listArrays =	QQJsonAnalyzer.friendsJsonToHasArr((ModelResult)object);
					
						sendMsg(handler, Constants.MSG_GETFOLLOWLIST_SUCCESS, listArrays);
				}
			}, null, 4);
		}
		//获取用户关注列表
				public static void getUserFollowers(final Context context,String openid,int reqnum,int sIndex,final Handler handler)
				{
					FriendAPI api=new FriendAPI(new AccountModel(GlobalVar.QQToken));
					api.friendFansList(context, "json", 50, sIndex, 0, 0, 0, new HttpCallback() {
						
						@Override
						public void onResult(Object object) {
							List<HashMap<String, String>> listArrays =	QQJsonAnalyzer.friendsJsonToHasArr((ModelResult)object);
							
								sendMsg(handler, Constants.MSG_GETFOLLOWERS_SUCCESS, listArrays);
						}
					}, null, 4);
				}
	    //判断A是否关注B
		public static boolean followed(final Context context, final String uid)
		{
			final boolean rs[]=new boolean[1];
			rs[0]=false;
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					FriendAPI api=new FriendAPI(new AccountModel(GlobalVar.QQToken));
					api.friendCheck(context, "json", "", uid, 1, new HttpCallback() {
						
						@Override
						public void onResult(Object object) {
							ModelResult result =(ModelResult)object;
							synchronized (rs) {
								if(!result.isSuccess())
								{
									rs[0]=false;
									
								}
								else
								{
								try {
									String strResult = new JSONObject(result.getObj().toString()).getJSONObject("data").getString(uid);
								    if(strResult.equals("true"))
								    	rs[0]=true;
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								}
								rs.notifyAll();
							}
							
							
							
							
						}
					}, null, 4);
				}
			});
			thread.start();
			synchronized (rs) {
			  try {
				rs.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			}
			return rs[0];
		}
	 //获取话题微博
	public static void  getTopicTimeLine(final Context context,String tId,String title,final Handler handler)
	{
		if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
			return;
		if(tId==null)
		{
			tId="0";
		}
		title = URLEncoder.encode(title);
		TimeLineAPI api =new TimeLineAPI(new AccountModel(GlobalVar.QQToken));
		api.getHTTimeLine(context, "json", 50, null, null, 0, 0, title, tId, 0, 0, new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				ModelResult result = (ModelResult)object;
				
				List<HashMap<String, String>> list =   QQJsonAnalyzer.timeLineJsonToHasArr(result);
				if(list==null)
					return;
				QQAPI.sendMsg(handler, Constants.MSG_GETTIMELINE_SUCCESS, list);
			}
		}, null, 4);
		
		
		
	}

	
	    //获取单个用户资料
		public static void  getUserInfo(final Context context,String openId,String name,final Handler handler)
		{
			if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
				return;
			UserAPI api =new UserAPI(new AccountModel(GlobalVar.QQToken));
			api.getUserOtherInfo(context, "json", name, openId, new HttpCallback() {
				
				@Override
				public void onResult(Object object) {
					List<HashMap<String, String>> listArrays =	QQJsonAnalyzer.userJsonToHasArr((ModelResult)object);
					if (listArrays==null) {
						Toast.makeText(context, "获取用户资料失败", 1000).show();
						((Activity)context).finish();
					}
					else
					{
						sendMsg(handler, Constants.MSG_GETUSERINFO_SUCCESS, listArrays);
					}
				}
			}, null, 4);
			
			
		}
	
	//获取公共时间线
	public static void  getPubline(final Context context,final Handler handler,String eTimeLine,int count)
	{
		int pageflag=0;
		if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
			return;
		if(eTimeLine!="0")
		{
		eTimeLine = eTimeLine.substring(0,eTimeLine.length()-3);
		pageflag=1;
		}
		TimeLineAPI api = new TimeLineAPI(new AccountModel(GlobalVar.QQToken));
		api.getPubTimeLine(context,pageflag,eTimeLine,count,0,"json" , new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				ModelResult result = (ModelResult)object;
				JSONObject jsonObject =(JSONObject)result.getObj();
				Log.v("qqstatus", jsonObject.toString());
				List<HashMap<String, String>> list =   QQJsonAnalyzer.timeLineJsonToHasArr(result);
				QQAPI.sendMsg(handler, Constants.MSG_GETTIMELINE_SUCCESS, list);
			
				
			}
		}, null, 4);
		
	}
	//获取用户时间线
			public static void  getUserline(final Context context,String openId,String nick,final Handler handler,String eTimeLine,int count)
			{
				if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
					return;
				if(eTimeLine!="0")
					eTimeLine = eTimeLine.substring(0,eTimeLine.length()-3);
				TimeLineAPI api = new TimeLineAPI(new AccountModel(GlobalVar.QQToken));
				api.getUserTimeLine(context, 1,Integer.parseInt(eTimeLine), count, 0, nick, openId, 0, 0, "json",new HttpCallback() {
					
					@Override
					public void onResult(Object object) {
						ModelResult result = (ModelResult)object;
						JSONObject jsonObject =(JSONObject)result.getObj();
						Log.v("qqstatus", jsonObject.toString());
						List<HashMap<String, String>> list =   QQJsonAnalyzer.timeLineJsonToHasArr(result);
						QQAPI.sendMsg(handler, Constants.MSG_GETTIMELINE_SUCCESS, list);
						
					}
				}, null, 4);
				
			}
	
	//获取@我时间线
		public static void  getMentionline(final Context context,final Handler handler,int pageflag,String eTimeLine,int count)
		{
			if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
				return;
			if(eTimeLine!="0")
				eTimeLine = eTimeLine.substring(0,eTimeLine.length()-3);
			TimeLineAPI api = new TimeLineAPI(new AccountModel(GlobalVar.QQToken));
			api.getMentionTimeLine(context, 1, Integer.parseInt(eTimeLine),0, count, 0, 0, "json", new HttpCallback() {
				
				@Override
				public void onResult(Object object) {
					ModelResult result = (ModelResult)object;
					JSONObject jsonObject =(JSONObject)result.getObj();
					Log.v("qqstatus", jsonObject.toString());
					List<HashMap<String, String>> list =   QQJsonAnalyzer.timeLineJsonToHasArr(result);
					QQAPI.sendMsg(handler, Constants.MSG_GETTIMELINE_SUCCESS, list);
				
					
				}
			}, null, 4);
		}
	//发表微博
	public static void sendWeibo(String content,final Context context)
	{
		if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
			return;
		WeiboAPI api = new WeiboAPI(new AccountModel(GlobalVar.QQToken));
		api.addWeibo(context,
				content, 
				"json", 
				0,
				0,
				1,
				0, 
				new HttpCallback() {
			
			@Override
			public void onResult(Object arg0) {
				ModelResult result =(ModelResult)arg0;
				if(result.isSuccess())
				{
					Toast.makeText(context, "发表成功", 1000).show();
					EditText etWriContent =(EditText)((Activity)context).findViewById(com.example.test.R.id.etwricontent);
					etWriContent.setText("");
				}
				else
				{
					Toast.makeText(context, "发表失败", 1000).show();
				}
			}
		}, null, 4);
	}
	//获取朋友时间线
	public static void  getFriendline(final Context context,final Handler handler,int pageflag,String eTimeLine)
	{
		if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
			return;
		if(eTimeLine!="0")
		eTimeLine = eTimeLine.substring(0,eTimeLine.length()-3);
		TimeLineAPI api = new TimeLineAPI(new AccountModel(GlobalVar.QQToken));
		api.getHomeTimeLine(context, pageflag, Integer.parseInt(eTimeLine),5, 0, 0, "json", new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				ModelResult result = (ModelResult)object;
				JSONObject jsonObject =(JSONObject)result.getObj();
				Log.v("qqstatus", jsonObject.toString());
				List<HashMap<String, String>> list =   QQJsonAnalyzer.timeLineJsonToHasArr(result);
				QQAPI.sendMsg(handler, Constants.MSG_GETTIMELINE_SUCCESS, list);
			
				
			}
		}, null, 4);
	}
	
	//评论微博
	public static void comment(final Context context,String reId,String content,final Handler handler)
	{
		if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
			return;
		WeiboAPI api =new WeiboAPI(new AccountModel(GlobalVar.QQToken));
		api.commentWeibo(context, reId, content, "json", 0, 0, 1, 0, new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				ModelResult result =(ModelResult)object;
				if(result.isSuccess())
				{
					JSONObject jsonObject =(JSONObject) result.getObj();
					Message msg=new Message();
					msg.what=Constants.MSG_COMMENT_SUCCESS;
					try {
						msg.obj="QQ"+jsonObject.getJSONObject("data").getString("id");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					handler.sendMessage(msg);
				}
				else
				{
					handler.sendEmptyMessage(Constants.MSG_COMMENT_FAIL);
				}
				
			}
		}, null, 4);
	}
	//转发微博
	public static void repost(final Context context,String reId,String content,final Handler handler)
	{
		if(!(GlobalVar.enWhich.equals("all")||GlobalVar.enWhich.equals("QQ")))
			return;
		WeiboAPI api =new WeiboAPI(new AccountModel(GlobalVar.QQToken));
		api.repostWeibo(context, reId, content, "json", 0, 0, 1, 0, new HttpCallback() {
			
			@Override
			public void onResult(Object object) {
				ModelResult result =(ModelResult)object;
				if(result.isSuccess())
				{
					JSONObject jsonObject =(JSONObject) result.getObj();
					Message msg=new Message();
					msg.what=Constants.MSG_REPOST_SUCCESS;
					try {
						msg.obj="QQ"+jsonObject.getJSONObject("data").getString("id");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					handler.sendMessage(msg);
				}
				else
				{
					handler.sendEmptyMessage(Constants.MSG_REPOST_FAIL);
				}
				
			}
		}, null, 4);
	}
	public static  void sendMsg(Handler handler,int msgType,Object obj)
 	{
 		    Message  msg =new Message();
			msg.what=msgType;
			msg.obj=obj;
			handler.sendMessage(msg);
 	}
	
}
